import scrapy
import re
import time
import json
from ..items import BiliJsonItem
import time
class GoSpider(scrapy.Spider):
    name = 'go'
    #allowed_domains = ['bilibili.com']
    start_urls = ['https://api.bilibili.com/x/web-interface/search/type?context=&page=1&order=&keyword=%E8%94%A1%E5%BE%90%E5%9D%A4&duration=&tids_1=&tids_2=&__refresh__=true&_extra=&search_type=video&highlight=1&single_column=0']

    def parse(self, response):
        for i in range(1,50):
            yield scrapy.Request(url = re.sub('page=(\d+)&','page='+str(i)+'&',response.url),callback=self.paqu)
    def paqu(self,response):
        def tm(num):
            tt = time.localtime(num)
            t1 = time.strftime('%Y-%m-%d',tt)
            return t1
        item = BiliJsonItem()
        jd = json.loads(response.text)
        for i in jd['data']['result']:
            item['author']=i['author']#作者名
            item['pubdate']=tm(i['pubdate'])#发布日期
            item['senddate']=tm(i['senddate'])#最后评论日期
            item['duration']=i['duration']#时长
            yield item






